package com.sskj.gcc.ui.fragment;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.gcc.R;
import com.sskj.gcc.bean.GuideListBean;
import com.sskj.gcc.bean.ZixunListBean;
import com.sskj.gcc.component.DaggerUserDataComponent;
import com.sskj.gcc.presenter.DiscoveryFragmentPresenter;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.decoration.DividerLineItemDecoration;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.m.bean.AgentNumBean;
import com.sskj.gocoin.mine.MyShareActivity;
import com.sskj.gocoin.utils.TipUtil;
import com.sskj.gocoin.v.activity.ApplyForShopActivity;
import com.sskj.gocoin.v.activity.MainActivity;
import com.sskj.gocoin.v.activity.SellerApplyActivity;
import com.sskj.gocoin.v.activity.WebViewActivity;
import com.sskj.gocoin.v.base.BaseFragment;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.zzhoujay.richtext.RichText;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import me.jessyan.autosize.utils.ScreenUtils;

@Route(path = RConfig.APP_DISCOVERY)
public class DiscoveryFragment extends BaseFragment<MainActivity, DiscoveryFragmentPresenter> {

    @BindView(R.id.toolBarLayout)
    ToolBarLayout toolBarLayout;
    @BindView(R.id.llMore)
    LinearLayout llMore;
    @BindView(R.id.llXszd)
    LinearLayout llXszd;
    @BindView(R.id.llTtwk)
    LinearLayout llTtwk;
    @BindView(R.id.llSjrz)
    LinearLayout llSjrz;
    @BindView(R.id.llSqzss)
    LinearLayout llSqzss;
    @BindView(R.id.llXtzz)
    LinearLayout llXtzz;
    @BindView(R.id.llJyzn)
    LinearLayout llJyzn;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private SlimAdapter<ZixunListBean> slimAdapter;
    private AlertDialog dialog;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_discovery;
    }

    @Override
    public DiscoveryFragmentPresenter getPresenter() {
        return new DiscoveryFragmentPresenter();
    }

    @Override
    public void initView() {
        toolBarLayout.setTitle(App.INSTANCE.getString(R.string.appJavaMainActivity9));
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setFirstDraw(false)
                .setLastDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 2))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.app_divider))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_zixun, new SlimInjector<ZixunListBean>() {
            @Override
            public void onInject(ZixunListBean data, IViewInjector injector, List payloads) {
                injector
                        .with(R.id.ivImg, view -> {
                            ImageView imageView = (ImageView) view;
                            ImageUtil.setRoundImage(data.getPic_addr(), imageView);
                        })
                        .text(R.id.tvTitle, data.getBm_title())
                        .text(R.id.tvTime, data.getIssue_time());
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView1, position, v) -> {
                    ARouter.getInstance()
                            .build(RConfig.APP_NOTICE_DETAIL)
                            .withString(Constans.HEAD, App.INSTANCE.getString(R.string.appJavaZixunListFragment2))
                            .withString(Constans.CONTENT, slimAdapter.getDataItem(position).getContent())
                            .withString(Constans.TIME, slimAdapter.getDataItem(position).getIssue_time())
                            .withString(Constans.TITLE, slimAdapter.getDataItem(position).getBm_title())
                            .navigation();
                });
    }

    @Override
    public void initData() {
        mPresenter.getData("1");
        //更多   跳转资讯页面
        ClickUtil.click(llMore, () -> {
            ARouter.getInstance().build(RConfig.APP_ZIXUN).navigation();
        });

        //新手指导
        ClickUtil.click(llXszd, () -> WebViewActivity.start(getActivity(), "xszd"));

        //天天挖矿
        ClickUtil.click(llTtwk, () -> {
            if (userData == null) {
                ARouter.getInstance().build(RConfig.APP_LOGIN).navigation();
                getActivity().overridePendingTransition(R.anim.lib_anim_in, R.anim.lib_anim_out);
                return;
            }
            ARouter.getInstance().build(RConfig.APP_MINING).navigation();
        });

        //商家认证
        ClickUtil.click(llSjrz, () -> {
            if (userData == null) {
                ARouter.getInstance().build(RConfig.APP_LOGIN).navigation();
                getActivity().overridePendingTransition(R.anim.lib_anim_in, R.anim.lib_anim_out);
                return;
            }
            //未绑定啥也不能干
            if (userData.getConfig().getGoogle_bind() == 0) {
                com.sskj.lib.util.TipUtil.googleBindTip(getActivity(), getString(R.string.lib_google_bind), () -> {
                    ARouter.getInstance().build(RConfig.MINE_SECURITY).navigation();
                });
                return;
            }
            if (!checkIdent(getActivity())) {//是否实名认证
                return;
            }
            if (userData.getConfig().getPayment_password_set()==0) {//是否设置资金密码
                ToastUtil.showShort(App.INSTANCE.getString(com.sskj.gocoin.R.string.strMainActivity218));
                ARouter.getInstance().build(RConfig.MINE_SECURITY).navigation();
                return;
            }

            if (userData.getIs_shop() == 2) {
                ToastUtil.showShort(getString(com.sskj.gocoin.R.string.my_sjshz));
                return;
            }
            if (userData.getIs_shop() == 1) {
                View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.app_dialog_unbound, null);
                dialog = new AlertDialog.Builder(getActivity(), R.style.custom_dialog)
                        .setView(dialogView)
                        .show();
                Window window = dialog.getWindow();
                int[] size = ScreenUtils.getScreenSize(getActivity());
                WindowManager.LayoutParams layoutParams = window.getAttributes();
                layoutParams.width = (int) (size[0] * 0.9f);
                window.setAttributes(layoutParams);
                TextView tvApply = dialogView.findViewById(R.id.tv_apply);
                TextView tvCancel = dialogView.findViewById(R.id.tv_cancel);
                ClickUtil.click(tvCancel, () -> dialog.dismiss());
                ClickUtil.click(tvApply, () -> mPresenter.requestRelieveBusiness());
                return;
            }
            if (!checkIdentHign(getActivity())) {
                return;
            }
            SellerApplyActivity.start(getActivity());
        });

        //个人推广
        ClickUtil.click(llSqzss, new ClickUtil.Click() {
            @Override
            public void click() {
                if (userData == null) {
                    ARouter.getInstance().build(RConfig.APP_LOGIN).navigation();
                    getActivity().overridePendingTransition(R.anim.lib_anim_in, R.anim.lib_anim_out);
                    return;
                }
//                WebViewActivity.start(getActivity(), "sqss");
                MyShareActivity.start(getActivity());
            }
        });

        //公告
        ClickUtil.click(llXtzz, () -> ARouter.getInstance().build(RConfig.APP_GONGGAO_LIST).navigation());
        //指南
        ClickUtil.click(llJyzn, () -> ARouter.getInstance().build(RConfig.APP_GUIDE).navigation());
    }


    public void updateUI(List<ZixunListBean> list) {
        slimAdapter.updateData(list);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getData("1");
        userViewModel.update();
    }

    public void onRelieveBusinessSuccess() {
        if (dialog != null) {
            dialog.dismiss();
        }
        userData.setIs_shop(0);
        userViewModel.update(userData);
    }

}
